package com.atguigu.springcloud.service.impl;

import com.atgui.springcloud.request.UserParam;
import com.atgui.springcloud.response.UserResult;
import com.atguigu.springcloud.dao.TblUserMapper;
import com.atguigu.springcloud.entities.TblUser;
import com.atguigu.springcloud.entities.TblUserExample;
import com.atguigu.springcloud.service.TestUserService;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

@Service("testUserService")
public class TestUserServiceImpl implements TestUserService {

    @Resource
    private TblUserMapper tblUserMapper;

    @Override
    public UserResult getOneUserData(UserParam param) {
        UserResult result = new UserResult();
        TblUserExample example = new TblUserExample();
        example.createCriteria().andSerialEqualTo(param.getSerial());
        List<TblUser> tblUsers = tblUserMapper.selectByExample(example);
        tblUsers.stream().forEach(item->{
            BeanUtils.copyProperties(item,result);
        });
        return result;
    }

    @Override
    public UserResult insertOneUser(UserParam param) {
        TblUser tblUser = new TblUser();
        tblUser.setSerial(param.getSerial());
        tblUser.setUserName(param.getUserName());
        tblUserMapper.insert(tblUser);

        UserResult result = new UserResult();
        TblUserExample example = new TblUserExample();
        example.createCriteria().andSerialEqualTo(param.getSerial());
        List<TblUser> tblUsers = tblUserMapper.selectByExample(example);
        UserResult userResult= new UserResult();
        userResult.setId(tblUsers.get(0).getId());
        userResult.setSerial(tblUsers.get(0).getSerial());
        userResult.setUserName(tblUsers.get(0).getUserName());

        return userResult;
    }
}
